package com.filldream.fastboot.common.okutil.stock.cf.kline;

import com.filldream.fastboot.common.constant.enums.StockFstEnum;
import com.filldream.fastboot.common.okutil.stock.BaseUtil;
import lombok.Data;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Data
public class KLineDTO {

    private List<KLineItemDTO> list;

    //分时图作日收盘价/五日图第一天收盘价
    private BigDecimal preClosePrice;

    //分时图日/五日图  的  价格或平均价格的最大涨跌幅
    private BigDecimal priceAndAvgPriceZdf;

    //股价精确位数
    private Integer priceDecimal;

    private String cfKLineType;

    private String codePrefix;


    public BigDecimal getMaxPrice(){
        if( preClosePrice != null ){
            if( cfKLineType.equals(StockFstEnum.five.getCfKType()) || cfKLineType.equals(StockFstEnum.min.getCfKType())){
                return preClosePrice.multiply(BigDecimal.ONE.add(priceAndAvgPriceZdf.abs().divide(new BigDecimal("100")))).setScale(priceDecimal, RoundingMode.HALF_UP);
            }
        }
        return BigDecimal.ZERO;
    }

    public BigDecimal getMINPrice(){
        if( preClosePrice != null ) {
            if (cfKLineType.equals(StockFstEnum.five.getCfKType()) || cfKLineType.equals(StockFstEnum.min.getCfKType())) {
                return preClosePrice.multiply(BigDecimal.ONE.subtract(priceAndAvgPriceZdf.abs().divide(new BigDecimal("100")))).setScale(priceDecimal, RoundingMode.HALF_UP);
            }
        }
        return BigDecimal.ZERO;
    }

    public void initPriceAndAvgPriceZdf(BigDecimal number){
        if(priceAndAvgPriceZdf == null || priceAndAvgPriceZdf.abs().compareTo(number.abs()) < 0){
            priceAndAvgPriceZdf = number;
        }
    }

    /**
     * 分时图的交易时间范围
     * @return
     */
    public List<String> getDateList(){
        String amStart = "09:30";
        String amEnd = "11:30";
        String pmStart = "13:00";
        String pmEnd = "15:00";
        if( codePrefix.equals("hk") ){
            amEnd = "12:00";
            pmEnd = "16:10";
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
        LocalTime start = LocalTime.parse(amStart, formatter);
        LocalTime end = LocalTime.parse(amEnd, formatter);
        List<String> timeList = new ArrayList<>();
        // 添加上午时间段
        while(start.isBefore(end) || start.equals(end)) {
            timeList.add(start.format(formatter));
            start = start.plusMinutes(1);
        }
        // 添加下午时间段
        start = LocalTime.parse(pmStart, formatter);
        end = LocalTime.parse(pmEnd, formatter);
        while(start.isBefore(end) || start.equals(end)) {
            timeList.add(start.format(formatter));
            start = start.plusMinutes(1);
        }
        return timeList;
    }

    public void initHumanVolume(){
        for (int i = 0; i < list.size(); i++) {
            KLineItemDTO item = list.get(i);
            item.setVolumeHuman(BaseUtil.autoNumberHuman(item.getVolume(),2));
            item.setDiffVolume( i == 0 ? BigDecimal.ZERO : item.getVolume().subtract(list.get(i-1).getVolume()) );
            item.setDiffVolumeHuman( BaseUtil.autoNumberHuman(item.getDiffVolume(),2) );
        }
    }


}
